package com.hundredapp.oa.repository;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.views.jsp.ui.SelectTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.GenericDao;
import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaAction;
import com.hundredapp.oa.domain.OaRole;
import com.hundredapp.oa.domain.OaRoleService;
import com.hundredapp.oa.domain.OaService;
import com.hundredapp.oa.domain.OaUser;
import com.hundredapp.oa.vo.MenuItem;
import com.hundredapp.oa.vo.ServiceVO;

@Repository
public class UserManagerImpl implements UserManager {

	@Autowired
	private GenericDao<OaUser, Integer> userDao;
	@Autowired
	private GenericDao<OaAction, Integer> actionDao;
	@Autowired
	private GenericDao<OaRoleService, Integer> roleServiceDao;

	public GenericDao<OaUser, Integer> getUserDao() {
		return userDao;
	}

	public void setUserDao(GenericDao<OaUser, Integer> userDao) {
		this.userDao = userDao;
	}

	public GenericDao<OaAction, Integer> getActionDao() {
		return actionDao;
	}

	public void setActionDao(GenericDao<OaAction, Integer> actionDao) {
		this.actionDao = actionDao;
	}

	@Override
	public int add(OaUser user) {
		return userDao.save(user);
	}

	public List<MenuItem> findMenu(int id) {
		List<MenuItem> menu = new ArrayList<MenuItem>();
		List<OaAction> level1 = new ArrayList<OaAction>();
		List<OaAction> level2 = new ArrayList<OaAction>();
		List<OaRole> roles = userDao.findById(id).getOaRoles();
		MenuItem temp1;
		MenuItem temp2;
		List<MenuItem> templist;
		level1 = actionDao.find("leaf != 1");
		for (OaRole role : roles) {
			for (OaAction action : role.getOaActions()) {
				if (action.getLeaf() == 1) {
					level2.add(action);
				}
			}
		}
		for (OaAction action1 : level1) {
			temp1 = new MenuItem();
			temp1.setText(action1.getActionText());
			temp1.setId(action1.getMappingAction());
			temp1.setCls("folder");
			temp1.setIndex(action1.getIndex());
			templist = new ArrayList<MenuItem>();
			for (OaAction action2 : level2) {
				if (action2.getParentId() == action1.getId()
						&& action2.getVisible() == 1) {
					boolean exist=false;
					for(MenuItem mi:templist){
						if(mi.getId()==action2.getMappingAction()){
							exist=true;
						}
					}
					if(!exist){
						temp2 = new MenuItem();
						temp2.setText(action2.getActionText());
						temp2.setId(action2.getMappingAction());
						temp2.setHrefTarget(action2.getMappingAction());
						temp2.setCls("file");
						temp2.setIndex(action2.getIndex());
						temp2.setLeaf("true");
						templist.add(temp2);
						temp1.setIndex(action1.getIndex());
					}
				}
			}
			if (!templist.isEmpty()) {
				temp1.setChildren(templist);
				menu.add(temp1);
			}
		}
		return menu;
	}
	
	@Override
	public OaUser findUserById(int id) {
		return userDao.findById(id);
	}

	@Override
	public OaUser login(String username, String password) {
		return userDao.findUnique(String.format("username='%s' and password='%s'",username,password));
	}

	@Override
	public List<OaAction> findAction(int id) {
		List<OaAction> result=new ArrayList<OaAction>();
		List<OaRole> roles = userDao.findById(id).getOaRoles();
		for (OaRole role : roles) {
			result.addAll(role.getOaActions());
		}
		return result;
	}
	
	public List<OaUser> findAllUser(String condition,int start,int limit){
		return userDao.find(condition,start,limit);
	}
	
	public List<ServiceVO> findService(int id){
		List<ServiceVO> result=new ArrayList<ServiceVO>();
		List<OaRole> roles = userDao.findById(id).getOaRoles();
		for (OaRole role : roles) {
			for (OaService service : role.getOaServices()) {
				ServiceVO temp=new ServiceVO();
				temp.setManagerName(service.getManagerName());
				temp.setServiceText(service.getServiceText());
				temp.setPermission(roleServiceDao.findUnique(
						String.format("serviceId='%s' and roleId='%s'",service.getId(),id)).getPermission());
				result.add(temp);
			}
		}
		return result;
	}

	@Override
	public void delete(int id) {
		userDao.remove(userDao.findById(id));
	}

	@Override
	public void update(OaUser user) {
		userDao.modify(user);
		
	}
	
	public int findAllCount(String condition){
		return userDao.count(condition);
	}
	
	public void setRoleServiceDao(GenericDao<OaRoleService, Integer> roleServiceDao) {
		this.roleServiceDao = roleServiceDao;
	}
}
